using System.Collections;
using System.Collections.Generic;
using Spine.Unity;
using UnityEngine;
using UnityEngine.Serialization;

[System.Serializable]
public class SpineTouchConfig
{
    [Header("要交互的骨骼")]
    [SpineSkeletonAttribute]
    public string skeleton;

    [Header("按钮类型")]
    public SpineTouchType touchType = SpineTouchType.Button;
    [Tooltip("只在按钮类型时生效")]
    public Vector2 size = Vector2.one;
    [Tooltip("只在按钮类型时生效")]
    public Vector3 eulerAngles = Vector3.zero;
    [Tooltip("只在碰撞体类型时生效")]
    public string boundingBoxName;
    
    #region 点击事件
    
    [Header("点击事件")] 
    //点击事件
    public SpineTouchEventInvokeNum invokeNum = SpineTouchEventInvokeNum.Single;
    public List<SpineTouchEvent> events = new ();

    /// <summary>
    /// 有一个事件可以执行就是未锁定的
    /// </summary>
    /// <param name="skeletonGraphic"></param>
    /// <param name="dynamicObject"></param>
    /// <returns></returns>
    public bool IsUnLock(SkeletonGraphic skeletonGraphic, DynamicObject dynamicObject)
    {
        if (events == null || events.Count == 0)
        {
            return false;
        }
        
        foreach (var touchEvent in events)
        {
            if (touchEvent.IsUnLock(skeletonGraphic,dynamicObject))
            {
                return true;
            }
        }

        return false;
    }

    #endregion

    #region 拖拽事件

    //TODO:添加拖拽事件

    #endregion
}

[System.Serializable]
public class SpineTouchConfigGroup
{
    public string groupName;
    public List<SpineTouchConfig> configs = new List<SpineTouchConfig>();
}

[System.Serializable]
public class SpineTouchEvent
{
    [Header("事件的前提条件")] 
    [Tooltip("这个动画事件没有前提条件")]
    public bool noCondition;
    public SpineTouchAnimation conditionAnimation;
    [Header("需要的参数")]
    public List<SpineTouchParamterState> paramterStates = new();
    
    [Header("点击播放的动画")] 
    public List<SpineTouchAnimationAdd> playAnimations;
    [Header("改变的参数")]
    public List<SpineTouchParamterState> changeParamter = new();

    public bool IsUnLock(SkeletonGraphic skeletonGraphic, DynamicObject dynamicObject)
    {
        if (!noCondition)
        {
            //检查参数是否正确
            foreach (var paramterState in paramterStates)
            {
                if (!dynamicObject.Equals(paramterState.paramterName, paramterState.FieldData))
                {
                    return false;
                }
            }
            
            //检查动画是否正确
            var track = skeletonGraphic.AnimationState.GetCurrent((int)conditionAnimation.layer);
            if(track != null && track.Animation.Name == conditionAnimation.animation)
                return true;
        }
        return false;
    }
}

[System.Serializable]
public class SpineTouchParamterState
{
    [SpineParamterStateAttribute]
    public string paramterName;
    public string paramterValue;

    public IFieldData FieldData;

    public void Init(DynamicObject dynamicObject)
    {
        FieldData = IFieldData.CreateFieldData(dynamicObject.GetFieldType(paramterName), paramterValue);
        //paramterValue = null;
    }
}

[System.Serializable]
public class SpineTouchAnimation
{
    public SpineTouchAnimationIndex layer;
    [SpineAnimationAttribute]
    public string animation;
}

[System.Serializable]
public class SpineTouchAnimationAdd : SpineTouchAnimation
{
    public SpineTouchAnimationAdd()
    {
        layer = SpineTouchAnimationIndex.Overlay;
    }

    [Tooltip("如果没有end动画，true：保持结束帧的状态，false：置空")]
    public bool stay;

    [Tooltip("这个动画必须播放完成才能播放其他动画")]
    public bool forceComplete;
    
    [Tooltip("动画播放完之后的动画，没有不用填")]
    [SpineAnimationAttribute]
    public string endAnimation;
}

public enum SpineTouchAnimationIndex
{
    Base = 0,
    Overlay = 1,
}

public enum SpineTouchEventInvokeNum
{
    Single = 1,
    Multiple = 2,
}

public enum SpineTouchType
{
    Button = 0,
    Collider = 1,
}

[System.Serializable]
public class SpineAnimLayer
{
    [SpineAnimationAttribute]
    public string animName;
    public int layer;
}